package com.blockchain.front.servlets.financial;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.ChargeType;
import com.blockchain.framework.config.ConfigureProvider;
import com.blockchain.framework.http.servlet.annotation.PagingServlet;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.resource.ResourceRegister;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.financial.FinancialManage;
import com.blockchain.util.parser.BigDecimalParser;
import com.blockchain.util.parser.EnumParser;
@PagingServlet(itemServlet = CnyCz.class)
public class CnyCz extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		BigDecimal sjxs=BigDecimalParser.parse(request.getParameter("sjxs"));
		BigDecimal czje=BigDecimalParser.parse(request.getParameter("czje")).add(sjxs);
		String name=request.getParameter("name");
		String zh=request.getParameter("zh");
		ChargeType type=EnumParser.parse(ChargeType.class, request.getParameter("type"));
		FinancialManage manage=serviceSession.getService(FinancialManage.class);

		if (null==name||name.equals("")) {
			name="未填写";
		}
		if (null==zh||zh.equals("")) {
			zh="未填写";
		}

		ConfigureProvider configureProvider = ResourceRegister.getResourceProvider(getServletContext()).getResource(ConfigureProvider.class);
		
		BigDecimal usdRate = new BigDecimal(configureProvider.get("SYSTEM.CNY_EUR_RATE"));
		
		int ddid=manage.addCz(type, name, zh, czje, usdRate);
		getController().prompt(request, response, PromptLevel.INFO,"订单创建成功，请转账。");
			String url = "";
			if (type == ChargeType.QR) {
				url=getController().getViewURI(request,CnyCz.class)+"?ddid="+ddid+"&cjje="+czje + "&type=qr";	
			} else {
				url=getController().getViewURI(request,CnyCz.class)+"?ddid="+ddid+"&cjje="+czje;
			}
		sendRedirect(request, response,url);
	}

}
